package com.choosefine.it.manager.service;

import java.util.List;
import java.util.Map;

import com.choosefine.it.base.BaseService;
import com.choosefine.it.basedata.service.CountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.manager.dao.ManagerPublishFindTeamInfoMapper;
import com.choosefine.it.manager.model.ManagerPublishFindTeamInfo;
import com.choosefine.it.manager.model.vo.ManagerPublishFindTeamInfoVO;
import com.choosefine.it.team.dao.TeamProjectCollectionMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 
 * @author juchengyang 2017年3月4日
 *
 */
@Service("managerPublishFindTeamInfoService")
public class ManagerPublishFindTeamInfoService extends BaseService {
	
	@Autowired 
	private ManagerPublishFindTeamInfoMapper managerPublishFindTeamInfoMapper;
	
	@Autowired
	MongoTemplate mongoTemplate;
	@Autowired
	private TeamProjectCollectionMapper teamProjectCollectionMapper;
	@Autowired
	CountService countService;
	
	/**
	 * 添加项目经理找班组发布信息
	 *  juchengyang 2017年3月4日
	 */
	public int insertManagerPublishFindTeamInfo(ManagerPublishFindTeamInfo model) {
		//userCode 校验
		userInfoUserCodeCheck(model.getUserCode());
		int num = 0;
		if (model.getPublisher() != null) {
			model.setVerifyStatus(CommonConstant.VERIFY_STATUS_1);
		} else {
			model.setVerifyStatus(CommonConstant.VERIFY_STATUS_0);
		}
		model.setIsValid(CommonConstant.VALID);
		model.setCtime(System.currentTimeMillis());
		model.setPublishTime(System.currentTimeMillis());
		model.setPublishStatus(0);
		model.setReleaseNo("MFT"+ System.currentTimeMillis());
		num += managerPublishFindTeamInfoMapper.insertSelective(model);
		if (num > 0){
			mongoTemplate.insert(model);
		}
		return num;
	}
	
	/**
	 * 删除发布信息（可批量）
	 *  juchengyang 2017年3月4日
	 */
	public int deleteManagerPublishFindTeamInfo (Map<String, Object> params) {
		int num = 0;
		num += managerPublishFindTeamInfoMapper.deleteManagerPublishTeam(params);
		if (num > 0){
			List<Integer> ids = (List<Integer>)params.get("ids");
			for(Integer id: ids){
				Update update = new Update();
				update.set("isValid", 1);
				update.set("utime", params.get("utime"));
				mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(id)), update, ManagerPublishFindTeamInfo.class);
			}
		}
		return num;
	}
	
	/**
	 * 根据id 查询发布详情
	 *  juchengyang 2017年3月4日
	 */
	public ManagerPublishFindTeamInfo selectManagerPublishFindTeamById (Integer id) {
		return mongoTemplate.findOne(new Query(Criteria.where("_id").is(id)), ManagerPublishFindTeamInfo.class);
	}
	
	/**
	 * 按条件查找发布列表
	 *  juchengyang 2017年3月4日
	 */
	public Page<ManagerPublishFindTeamInfo> selectManagerPublishTeamByCondition (Map<String, Object> params, Integer pageNo, Integer pageSize){
		Page<ManagerPublishFindTeamInfo> result = null;
		PageHelper.startPage(pageNo, pageSize);
		result = managerPublishFindTeamInfoMapper.selectManagerPublishTeamByCondition(params);
		for (int i = 0; i < result.size(); i++){
			result.set(i, mongoTemplate.findOne(new Query(Criteria.where("_id").is(result.get(i).getId())), ManagerPublishFindTeamInfo.class));
		}
		return result;
	}
	
	/**
	 * app 发布管理 查询发布的招班组信息
	 *  juchengyang 2017年3月4日
	 */
	public Page<ManagerPublishFindTeamInfo> selectManagerPublishFindTeamByUserCode (Map<String, Object> params, Integer pageNo, Integer pageSize){
		PageHelper.startPage(pageNo, pageSize);
		Page<ManagerPublishFindTeamInfo> result = managerPublishFindTeamInfoMapper.selectManagerPublishFindTeamByUserCode(params);
		for (int i = 0; i < result.size(); i++){
			result.set(i, mongoTemplate.findOne(new Query(Criteria.where("_id").is(result.get(i).getId())), ManagerPublishFindTeamInfo.class));
		}
		return result;
	}
	
	/**
	 * 控制台 发布审核
	 *  juchengyang 2017年3月4日
	 */
	public int updateManagerPublishFindTeamInfoVerifyStatus (Map<String ,Object> params) {
		int num = 0;
		num += managerPublishFindTeamInfoMapper.updateManagerPublishFindTeamInfoVerifyStatus(params);
		if (num > 0){
			Update update = new Update();
			update.set("verifyStatus", params.get("verifyStatus"));
			update.set("publisher", params.get("publisher"));
			update.set("utime", params.get("utime"));
			if (params.containsKey("reason")){
				update.set("reason", params.get("reason"));
			}
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("id"))), update, ManagerPublishFindTeamInfo.class);
		}
		return num;
	}
	
	/**
	 * app 修改发布状态
	 *  juchengyang 2017年3月4日
	 */
	public int updateManagerPublishFindTeamPublishStatus (Map<String, Object> params){
		params.put("utime", System.currentTimeMillis());
		int num = managerPublishFindTeamInfoMapper.updateManagerPublishFindTeamPublishStatus(params);
		if (num > 0){
			Update update = new Update();
			update.set("publishStatus", params.get("publishStatus"));
			update.set("utime", params.get("utime"));
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("id"))), update, ManagerPublishFindTeamInfo.class);
		}
		return num;
	}
	/**
	 * 根据id 修改发布信息
	 *  juchengyang 2017年3月4日
	 */
	public int updateManagerPublishFindTeamInfo (ManagerPublishFindTeamInfo model) {
		int num = 0;
		model.setPublisher(null);
		model.setVerifyStatus(CommonConstant.VERIFY_STATUS_0);
		model.setUtime(System.currentTimeMillis());
		model.setReason(null);
		num += managerPublishFindTeamInfoMapper.updateByPrimaryKeySelective(model);
		if (num > 0){
			Update update = mongoUpdate(model);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(model.getId())), update, ManagerPublishFindTeamInfo.class);
		}
		return num;
	}

	private Update mongoUpdate(ManagerPublishFindTeamInfo model) {
		Update update = new Update();
		update.set("cityCode", model.getCityCode());
		update.set("cityName", model.getCityName());
		update.set("provinceCode", model.getProvinceCode());
		update.set("provinceName", model.getProvinceName());
		update.set("engCategoryId", model.getEngCategoryId());
		update.set("isValid", model.getIsValid());
		update.set("payMethod", model.getPayMethod());
		update.set("projectAddress", model.getProjectAddress());
		update.set("projectContact", model.getProjectContact());
		update.set("projectContactName", model.getProjectContactName());
		update.set("projectName", model.getProjectName());
		update.set("projectState", model.getProjectState());
		update.set("projectTypeId", model.getProjectTypeId());
		update.set("projectTypeName", model.getProjectTypeName());
		update.set("publisher", model.getPublisher());
		update.set("publishExplain", model.getPublishExplain());
		update.set("publishTime", model.getPublishTime());
		update.set("requirementContent", model.getRequirementContent());
		update.set("startTime", model.getStartTime());
		update.set("teamTypeIds", model.getTeamTypeIds());
		update.set("teamTypeName", model.getTeamTypeName());
		update.set("timeLimit", model.getTimeLimit());
		update.set("userCode", model.getUserCode());
		update.set("utime", model.getUtime());
		update.set("verifyStatus", model.getVerifyStatus());
		update.set("reason", model.getReason());
		update.set("constructionUnit", model.getConstructionUnit());
		update.set("constructionUnitCode", model.getConstructionUnitCode());
		update.set("constructUnit", model.getConstructUnit());
		update.set("constructUnitCode", model.getConstructUnitCode());
		update.set("phone1", model.getPhone1());
		update.set("phone2", model.getPhone2());
		return update;
	}
	public List<ManagerPublishFindTeamInfoVO> selectByContractAndRegionAndTeamType(Map<String, Object> map, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return managerPublishFindTeamInfoMapper.selectByContractAndRegionAndTeamType(map);
	};
	public ManagerPublishFindTeamInfo selectTeamJobsDetailById(Map map) {
		int count=countService.isInsert(map);
		ManagerPublishFindTeamInfo managerPublishFindTeamInfo=mongoTemplate.findOne(new Query(Criteria.where("_id").is(map.get("publishId"))), ManagerPublishFindTeamInfo.class);
		if(managerPublishFindTeamInfo!=null){
			managerPublishFindTeamInfo.setIsInsert(count);
		}
		return managerPublishFindTeamInfo;
	}
}
